<?php
/**
 * User: lsx
 * Date: 2017/4/21
 * Time: 14:32
 * Progress:0%
 */

namespace app\wechat\controller;


use library\tencent\wechat\admin_wechat\Settings;
use library\tencent\wechat\admin_wechat\WechatMessageHandler;
use think\Db;

class WechatApi
{
    //微信自动回复接口
    public function reply_api()
    {
        if (!isset($_GET['echostr'])) {
            $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
            $m = WechatMessageHandler::instance();
            $m->handle($xml, true);
        } else {
            $config=Settings::get('wechat_mp_config');
            $token=$config['token'];
            $this->checkSignature($token);
        }
    }

    public function checkSignature($token)
    {

        $signature = input("signature");
        $timestamp = input("timestamp");
        $nonce = input("nonce");
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);

        if ($tmpStr == $signature) {
            echo input("echostr");
        } else {
            return false;
        }
    }

}